//
//  GCMineMenuViewController.m
//  GoldChampion
//
//  Created by Mac on 2018/4/11.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "GCFindViewController.h"
#import "TMLoginViewController.h"
#import "JKRFallsLayout.h"
#import "GCHomeCollectionViewCell.h"
#import "UICollectionView+STNoresult.h"
#import "GCWokDetailViewController.h"
#import "GCTranceationingAnimation.h"
@interface GCFindViewController ()<JKRFallsLayoutDelegate,UICollectionViewDataSource,UINavigationControllerDelegate>
@property(nonatomic, strong) STSegementButton     *segementButton;
@property(nonatomic, strong) UIScrollView       *scrollView;
@property (nonatomic, weak)  UICollectionView    *collectionView;
@property(nonatomic, strong) NSMutableArray  <GCItemModel*>                   *dataSouce;
@property(nonatomic, strong) GCTranceationingAnimation                     *animationObject;
@end

@implementation GCFindViewController
#pragma mark --vc 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configSubView];


   // [self performSelector:@selector(sddsddsad) withObject:nil afterDelay:0];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //截图导航栏，放到view 上，导航栏之下 做动画
    UIView *existVIew = [self.view viewWithTag:9999];
    if (!existVIew) {
        UIView * copyNav = [self.navigationController.navigationBar snapshotViewAfterScreenUpdates:YES];
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth,  [TMUtils navgationBarBootom])];
        copyNav.bottom = view.height;
        view.backgroundColor = [UIColor whiteColor];
        view.tag = 9999;
        [view st_showBottomLine];
        [view addSubview:copyNav];
        [self.view addSubview:view];
    }
    //展示cell 的渐变动画
    GCHomeCollectionViewCell * cell = (id)self.imageView.superview;
    if (cell) {
        cell.iconImageView.alpha = 0;
        cell.titleLable.alpha = 0;
        [UIView animateWithDuration:0.25 animations:^{
            cell.iconImageView.alpha = 1;
            cell.titleLable.alpha = 1;
        }];
    }
    [self st_setLeftItemWithImage:[UIImage imageNamed:@"更多1"]];
    [self st_setRightItemWithImage:[UIImage imageNamed:@"视频1"]];
    [[STLayoutViewController defaultLayout] setPanGestureEnable:YES];
    return;
    if (![STUserManger defult].loginedUser) {
        [[STLayoutViewController defaultLayout] setPanGestureEnable:NO];
    }else{
        [[STLayoutViewController defaultLayout] setPanGestureEnable:YES];
    }
    

}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
     [[STLayoutViewController defaultLayout] setPanGestureEnable:NO];
    //隐藏icon 和lable
    GCHomeCollectionViewCell * cell = (id)self.imageView.superview;
    if (cell) {
        cell.iconImageView.alpha = 0;
        cell.titleLable.alpha = 0;
    }
}
- (void)configSubView{
    
    __weak typeof(self) weakSelf =  self;
    self.segementButton = [[STSegementButton alloc] initWithFrame:CGRectMake(0, [TMUtils navgationBarBootom], UIScreenWidth/2, 40)
                                                         andTitle:@[@"关注",@"发现",@"同城"] handle:^(STSegementButton *sender) {
                                                             if (weakSelf.segementButton.cureentIndex == 1) {
                                                                 [weakSelf.scrollView setContentOffset:CGPointMake(UIScreenWidth, 0) animated:YES];
                                                             }else if(weakSelf.segementButton.cureentIndex == 2){
                                                                 [weakSelf.scrollView setContentOffset:CGPointMake(2*UIScreenWidth, 0) animated:YES];
                                                             }
                                                             else if(weakSelf.segementButton.cureentIndex == 3){
                                                                 [weakSelf.scrollView setContentOffset:CGPointMake(3*UIScreenWidth, 0) animated:YES];
                                                             }
                                                             else{
                                                                 [weakSelf.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
                                                             }
                                                             // [weakSelf.tableView reloadData];
                                                         }];
    self.segementButton.backgroundColor = [UIColor whiteColor];
    self.segementButton.lineSelectedColor = TM_redColor;
    self.segementButton.butTitleSelectedColor = TM_redColor;
    self.segementButton.butTitleColor = TM_secendTextColor;
    self.segementButton.lineColor = [UIColor whiteColor];
    self.segementButton.cureentIndex = 0;
    self.segementButton.autoMoveWithClic = YES;
    self.navigationItem.titleView = self.segementButton;
    
    
    
    JKRFallsLayout *fallsLayout = [[JKRFallsLayout alloc] init];
    fallsLayout.delegate = self;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:fallsLayout];
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionView];
    _collectionView = collectionView;
    collectionView.dataSource = self;
    [collectionView registerClass:[GCHomeCollectionViewCell class] forCellWithReuseIdentifier:@"item"];
    
    self.collectionView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 400) title:@"" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendHomeListRequest];
    }];
    [self.collectionView.mj_header beginRefreshing];

}
- (void)configScrollView{
    __weak typeof(self) weakSelf =  self;
    self.segementButton = [[STSegementButton alloc] initWithFrame:CGRectMake(0, [TMUtils navgationBarBootom], UIScreenWidth/2, 40)
                                                         andTitle:@[@"关注",@"发现",@"同城"] handle:^(STSegementButton *sender) {
                                                             if (weakSelf.segementButton.cureentIndex == 1) {
                                                                 [weakSelf.scrollView setContentOffset:CGPointMake(UIScreenWidth, 0) animated:YES];
                                                             }else if(weakSelf.segementButton.cureentIndex == 2){
                                                                 [weakSelf.scrollView setContentOffset:CGPointMake(2*UIScreenWidth, 0) animated:YES];
                                                             }
                                                             else if(weakSelf.segementButton.cureentIndex == 3){
                                                                 [weakSelf.scrollView setContentOffset:CGPointMake(3*UIScreenWidth, 0) animated:YES];
                                                             }
                                                             else{
                                                                 [weakSelf.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
                                                             }
                                                             // [weakSelf.tableView reloadData];
                                                         }];
    self.segementButton.backgroundColor = [UIColor whiteColor];
    self.segementButton.lineSelectedColor = TM_redColor;
    self.segementButton.butTitleSelectedColor = TM_redColor;
    self.segementButton.butTitleColor = TM_secendTextColor;
    self.segementButton.lineColor = [UIColor whiteColor];
    self.segementButton.cureentIndex = 0;
    self.segementButton.autoMoveWithClic = YES;
    self.navigationItem.titleView = self.segementButton;
    
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, [TMUtils navgationBarBootom], UIScreenWidth, [TMUtils tabBarTop] - [TMUtils navgationBarBootom] + 49)];
    self.scrollView.scrollEnabled = NO;
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.contentSize = CGSizeMake(3 * UIScreenWidth, 0);
    [self.view addSubview:self.scrollView];
    
    UIViewController * vc =  [UIViewController new];
    [self addChildViewController:vc];
    UIView * view = vc.view;
    [self.scrollView addSubview:view];
    
    UIViewController * taijiVC = [UIViewController new];
    [self addChildViewController:taijiVC];
    UIView * listView = taijiVC.view;
    listView.left= UIScreenWidth;
    [self.scrollView addSubview:listView];
    
    
    UIViewController * zhizhiVC = [UIViewController new];
    [self addChildViewController:zhizhiVC];
    UIView * zhizhiView = zhizhiVC.view;
    zhizhiView.left= UIScreenWidth * 2;
    [self.scrollView addSubview:zhizhiView];
}
#pragma mark --UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return self.dataSouce.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GCHomeCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    cell.model = self.dataSouce[indexPath.row];
    __weak typeof(self) weakSelf =  self;
    [cell setImageViewSelectedHandle:^(UIImageView *imageView) {
        [weakSelf onSelctedImageView:imageView indexPath:indexPath];
    }];
    return cell;
}
#pragma mark --JKRFallsLayoutDelegate
- (CGFloat)columnMarginInFallsLayout:(JKRFallsLayout *)fallsLayout
{
    return 5;
}

- (CGFloat)rowMarginInFallsLayout:(JKRFallsLayout *)fallsLayout
{
    return 5;
}

- (CGFloat)columnCountInFallsLayout:(JKRFallsLayout *)fallsLayout
{
    return 2;
}

- (UIEdgeInsets)edgeInsetsInFallsLayout:(JKRFallsLayout *)fallsLayout
{
    return UIEdgeInsetsMake(20, 10, 20, 10);
}
#pragma mark --Action Method
- (void)onSelctedImageView:(UIImageView*)imageView indexPath:(NSIndexPath*)indexPath{
    GCWokDetailViewController * vc = [GCWokDetailViewController new];
    vc.imageView.image = imageView.image;
    self.imageView = imageView;
    self.navigationController.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
    DDLogInfo(@"点击了图片");
}
#pragma mark --UINavigationControllerDelegate
- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                            animationControllerForOperation:(UINavigationControllerOperation)operation
                                                         fromViewController:(UIViewController *)fromVC
                                                           toViewController:(UIViewController *)toVC  {
    
    if (fromVC == self && [toVC isKindOfClass:[GCWokDetailViewController class]]) {
        self.animationObject = [GCTranceationingAnimation new];
        return self.animationObject;
    }
    return nil;
}
- (void)st_leftBarAction:(id)sender{
    [[STLayoutViewController defaultLayout] showLeftViewController:YES];
    return;
    if (![STUserManger defult].loginedUser) {
        [self showAlertTitle:@"您还没有登录，请问要登录吗" message:@"" leftTitle:@"取消" rightTitle:@"确认" block:^(NSString *name) {
            if ([name isEqualToString:@"确认"]) {
                STNavigationController * nav = [[STNavigationController alloc] initWithRootViewController:[TMLoginViewController new]];
                [self presentViewController:nav animated:YES completion:nil];
            }
        }];
    }else{
       [[STLayoutViewController defaultLayout] showLeftViewController:YES];
    }
    
}
- (void)st_rightBarAction:(id)sender{

    if (![STUserManger defult].loginedUser) {
        [self showAlertTitle:@"您还没有登录，请问要登录吗" message:@"" leftTitle:@"取消" rightTitle:@"确认" block:^(NSString *name) {
            if ([name isEqualToString:@"确认"]) {
                STNavigationController * nav = [[STNavigationController alloc] initWithRootViewController:[TMLoginViewController new]];
                [self presentViewController:nav animated:YES completion:nil];
            }
        }];

    }else{
       // [[STLayoutViewController defaultLayout] showLeftViewController:YES];
    }
}
#pragma mark --NetWork Method
- (void)sendHomeListRequest{
    NSArray *array = [GCItemModel mj_objectArrayWithFilename:@"1.plist"];
    [TMUtils debugSimulationNetWorkWithHadle:^(NSInteger afterTime) {
        self.dataSouce = [array mutableCopy];
        [self.collectionView reloadData];
        [self.collectionView.mj_header endRefreshing];
    }];

}
@end

